﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.Commons;


namespace YT.Modules
{
    public partial class frmAirBoneGridResult : Form
    {

        ArrayList marrAir;
        ArrayList marrBone;
        bool mIsRight;

        int XSpe = 60;
        int YSpe = 30;
        int YList = 5;  //Y 轴上出来的一点点。 0 上面的那个距离
        int Xstart = 50;
        int Ystart = 80;
        ArrayList marrX1Loc = new ArrayList();   //用于保存实线的坐标
        ArrayList marrX2Loc = new ArrayList();   //用于保存虚线的坐标


        public frmAirBoneGridResult()
        {
            InitializeComponent();
        }

        private int XLocation(double dblResult)   //求X的比例
        {
            if (dblResult <= 0.125)
                return ((Point)marrX1Loc[0]).X;
            else if (dblResult <= 0.25)
                return ((Point)marrX1Loc[1]).X;
            else if (dblResult <= 0.5)
                return ((Point)marrX1Loc[2]).X;
            else if (dblResult <= 0.75)
                return ((Point)marrX2Loc[0]).X;
            else if (dblResult <= 1)
                return ((Point)marrX1Loc[3]).X;
            else if (dblResult <= 1.5)
                return ((Point)marrX2Loc[1]).X;
            else if (dblResult <= 2)
                return ((Point)marrX1Loc[4]).X;
            else if (dblResult <= 3)
                return ((Point)marrX2Loc[2]).X;
            else if (dblResult <= 4)
                return ((Point)marrX1Loc[5]).X;
            else if (dblResult <= 6)
                return ((Point)marrX2Loc[3]).X;
            else if (dblResult <= 8)
                return ((Point)marrX1Loc[6]).X;
            else
                return 0;
        }

        private int YScale()  //求Y的比例
        {
            return 30 / 10;

        }

        #region Air
        private void DrawAirRightUnMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            p1.Width = 2;
            g.DrawEllipse(p1, x - 4, y + Ystart - 8, 8, 16);
        }
        private void DrawAirLeftUnMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            Font fntString = new Font(Font.FontFamily, 16, FontStyle.Bold);
            g.DrawString("×", fntString, new SolidBrush(Color.Black), x -14, y + Ystart - 13);
        }

        private void DrawAirRightMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            Font fntString = new Font(Font.FontFamily, 14, FontStyle.Bold);
            g.DrawString("△", fntString, new SolidBrush(Color.Black), x - 12, y + Ystart - 12);
        }
        private void DrawAirLeftMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            p1.Width = 2;
            g.DrawRectangle(p1, x - 3, y + Ystart - 8, 8, 14);
        }
        #endregion
        #region Bone
        private void DrawBoneRightUnMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            Font fntString = new Font(Font.FontFamily, 18, FontStyle.Bold);
            g.DrawString("<", fntString, new SolidBrush(Color.Black), x - 20, y + Ystart - 12);
        }
        private void DrawBoneLeftUnMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            Font fntString = new Font(Font.FontFamily, 18, FontStyle.Bold);
            g.DrawString(">", fntString, new SolidBrush(Color.Black), x - 20, y + Ystart - 12);
        }

        private void DrawBoneRightMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            Font fntString = new Font(Font.FontFamily, 18, FontStyle.Bold);
            g.DrawString("[", fntString, new SolidBrush(Color.Black), x - 15, y + Ystart - 12);
        }
        private void DrawBoneLeftMask(float x, float y)
        {
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            Font fntString = new Font(Font.FontFamily, 18, FontStyle.Bold);
            g.DrawString("]", fntString, new SolidBrush(Color.Black), x - 15, y + Ystart - 12);
        }



        #endregion

        private void DrawGrid()
        {
            int YLen = 14 * 30;
            Graphics g = this.CreateGraphics();
            Pen p1 = new Pen(Color.Black);
            p1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

            Font fntString = new Font(Font.FontFamily, 8, FontStyle.Regular);

            Point pot1 = new Point(Xstart, Ystart - YList);
            Point pot2 = new Point(Xstart, Ystart + YLen);
            p1.Width = 2;
            g.DrawLine(p1, pot1, pot2);
            for (int i = 0; i <= 6; i++)
            {
                p1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                Point point1 = new Point(Xstart + 5 + i * XSpe, Ystart - YList);
                Point point2 = new Point(Xstart + 5 + i * XSpe, Ystart + YLen);
                p1.Width = 1;
                marrX1Loc.Add(point1);   //保存x的坐标
                Point pointString = new Point(Xstart + i * XSpe, Ystart - XSpe / 2);
                if (i == 0) g.DrawString(".125", fntString, new SolidBrush(Color.Red), pointString);
                if (i == 1) g.DrawString(".25", fntString, new SolidBrush(Color.Red), pointString);
                if (i == 2) g.DrawString(".5", fntString, new SolidBrush(Color.Red), pointString);
                if (i == 3) g.DrawString("1", fntString, new SolidBrush(Color.Red), pointString);
                if (i == 4) g.DrawString("2", fntString, new SolidBrush(Color.Red), pointString);
                if (i == 5) g.DrawString("4", fntString, new SolidBrush(Color.Red), pointString);
                if (i == 6) g.DrawString("8", fntString, new SolidBrush(Color.Red), pointString);

                g.DrawLine(p1, point1, point2);
            }
            p1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            Point pot3 = new Point(Xstart + 15 + 6 * XSpe, Ystart - YList);
            Point pot4 = new Point(Xstart + 15 + 6 * XSpe, Ystart + YLen);
            p1.Width = 2;
            g.DrawLine(p1, pot3, pot4);

            for (int j = 0; j <= 3; j++)
            {
                p1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                Point point1 = new Point(Xstart + 5 + XSpe / 2 + (j + 2) * XSpe, Ystart - YList);
                Point point2 = new Point(Xstart + 5 + XSpe / 2 + (j + 2) * XSpe, Ystart + YLen);
                marrX2Loc.Add(point1);   //保存x的坐标
                p1.Width = 1;
                g.DrawLine(p1, point1, point2);
                Point pointString = new Point(Xstart + 5 + 25 + (j + 2) * XSpe, Ystart - XSpe / 2);
                if (j == 0) g.DrawString(".75", fntString, new SolidBrush(Color.Red), pointString);
                if (j == 1) g.DrawString("1.5", fntString, new SolidBrush(Color.Red), pointString);
                if (j == 2) g.DrawString("3", fntString, new SolidBrush(Color.Red), pointString);
                if (j == 3) g.DrawString("6", fntString, new SolidBrush(Color.Red), pointString);

            }

            p1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            Point potY1 = new Point(Xstart, Ystart - 5);
            Point potY2 = new Point(Xstart - 45 + 7 * XSpe, Ystart - 5);
            p1.Width = 2;
            g.DrawLine(p1, potY1, potY2);
            p1.Width = 1;

            for (int x = 0; x <= 14; x++)
            {
                Point potY3 = new Point(Xstart, Ystart + x * YSpe);
                Point potY4 = new Point(Xstart - 45 + 7 * XSpe, Ystart + x * YSpe);
                if (x == 1 || x == 3 || x == 14)
                { p1.Width = 2; }
                else
                { p1.Width = 1; }
                g.DrawLine(p1, potY3, potY4);
                Point pointString = new Point(Xstart - 25, Ystart + x * YSpe - 5);
                if (x == 0) g.DrawString("-10", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 1) g.DrawString("  0", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 2) g.DrawString(" 10", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 3) g.DrawString(" 20", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 4) g.DrawString(" 30", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 5) g.DrawString(" 40", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 6) g.DrawString(" 50", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 7) g.DrawString(" 60", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 8) g.DrawString(" 70", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 9) g.DrawString(" 80", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 10) g.DrawString(" 90", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 11) g.DrawString("100", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 12) g.DrawString("110", fntString, new SolidBrush(Color.Red), pointString);
                if (x == 13) g.DrawString("120", fntString, new SolidBrush(Color.Red), pointString);
            }
        }

        private bool IsMark(double dblResult)
        {
            if (dblResult > 0 && dblResult <= 20)
                return true;
            else
                return false;
        }


        private void GetPoint(ref float x, ref float y)
        {
            x = XLocation(x);
            y = (y + 10) * YScale();
        }


        private void DrawImaige(int intType, bool blnIsRignt, float x, float y)
        {
            // intType=0 AIR
            // intType=1 Bone
            DrawGrid();
            if (intType == 0)  // AIR
            {
                if (IsMark(y))
                {
                    GetPoint(ref x, ref y);
                    if (blnIsRignt)
                        DrawAirRightMask(x, y);
                    else
                        DrawAirLeftMask(x, y);
                }
                else
                {
                    GetPoint(ref x, ref y);
                    if (blnIsRignt)
                        DrawAirRightUnMask(x, y);
                    else
                        DrawAirLeftUnMask(x, y);
                }
            }
            else if (intType == 1) //BONE
            {
                if (IsMark(y))
                {
                    GetPoint(ref x, ref y);
                    if (blnIsRignt)
                        DrawBoneRightMask(x, y);
                    else
                        DrawBoneLeftMask(x, y);
                }
                else
                {
                    GetPoint(ref x, ref y);
                    if (blnIsRignt)
                        DrawBoneRightUnMask(x, y);
                    else
                        DrawBoneLeftUnMask(x, y);
                }
            }

        }

        public void DrawImaginPic(ArrayList arrAir, ArrayList arrBone, bool IsRight)
        {
            marrAir = arrAir;
            marrBone = arrBone;
            mIsRight = IsRight;
            foreach (string strAir in arrAir)
            {
                DrawImaige(0, IsRight, float.Parse(strAir.ToString().Split('_')[0]), float.Parse(strAir.ToString().Split('_')[0]));
            }
        }

        private void frmAirBoneGridResult_Paint(object sender, PaintEventArgs e)
        {
            foreach (string strAir in marrAir)
            {
                string strX = strAir.ToString().Split('_')[0];
                string strY = strAir.ToString().Split('_')[1];
                DrawImaige(0, mIsRight, float.Parse(strX), float.Parse(strY ));
            }
            foreach (string strBone in marrBone)
            {
                string strX = strBone.ToString().Split('_')[0];
                string strY = strBone.ToString().Split('_')[1];
                DrawImaige(1, mIsRight, float.Parse(strX), float.Parse(strY));
            }
        }
    }
}
